﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using DiamondDash.Core;

namespace DiamondDash.Views
{
    public partial class DiamondItemView : UserControl
    {
        public int CurrentType { get; set; }

        public bool IsTooled { get; set; }

        public bool IsSence { get; set; }

        public int ToolType { get; set; }

        public DiamondItemView()
        {
            InitializeComponent();
            IsSence = false;
            this.StoryboardHideChange.Completed += StoryboardHideChange_Completed;
            this.StoryboardSuperDiamond.RepeatBehavior = RepeatBehavior.Forever;
        }

        void StoryboardHideChange_Completed(object sender, EventArgs e)
        {
            SetColor(CurrentType);
            StoryboardShowChange.Begin();
        }


        /// <summary>
        /// 设置颜色类型
        /// </summary>
        /// <param name="type">1-5是普通颜色宝石，6是超级钻石道具</param>
        public void SetColor(int type)
        {
            //switch (type)
            //{
            //    case 1:
            //        this.rect.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
            //        break;
            //    case 2:
            //        this.rect.Fill = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
            //        break;
            //    case 3:
            //        this.rect.Fill = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
            //        break;
            //    case 4:
            //        this.rect.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 255, 0));
            //        break;
            //    case 5:
            //        this.rect.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 255));
            //        break;
            //}
            IsSence = false;
            IsTooled = false;
            CurrentType = type;
            this.StoryboardSuperDiamond.Stop();
            if (type >= 1 && type <= 5)
            {
                this.itemImage.Visibility = System.Windows.Visibility.Visible;
                this.gridSuperDiamond.Visibility = System.Windows.Visibility.Collapsed;
                this.toolImage.Visibility = System.Windows.Visibility.Collapsed;
                this.path1.Visibility = System.Windows.Visibility.Collapsed;
                this.StoryboardToolSence.Stop();
                this.StoryboardTool.Stop();
                this.itemImage.Opacity = 1;
                string uri = uri = "/Images/diamond" + type + ".png";
                BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
                itemImage.Source = bitmap;
            }
            else
            {
                this.gridSuperDiamond.Visibility = System.Windows.Visibility.Visible;
                this.itemImage.Visibility = System.Windows.Visibility.Collapsed;
                this.toolImage.Visibility = System.Windows.Visibility.Collapsed;
                this.path1.Visibility = System.Windows.Visibility.Collapsed;
                this.StoryboardToolSence.Stop();
                this.StoryboardTool.Stop();
            }
        }

        public void Show()
        {
            this.StoryboardShow.Begin();
        }

        public void Hide()
        {
            this.LayoutRoot.Opacity = 0;
            //this.StoryboardHide.Begin();
        }


        internal void ChangeColor(int type)
        {
            SetColor(type);
            //CurrentType = type;
            //StoryboardHideChange.Begin();
        }

        public void UseSuperDiamond()
        {
            this.StoryboardSuperDiamond.Stop();
            this.gridSuperDiamond.Visibility = System.Windows.Visibility.Collapsed;
            this.itemImage.Visibility = System.Windows.Visibility.Visible;
        }

        public void ShowSuperDiamond(int row, int column)
        {
            this.gridSuperDiamond.Visibility = System.Windows.Visibility.Visible;
            this.StoryboardSuperDiamond.Begin();

        }

        public bool ClearToolItem()
        {
            if (IsTooled)
            {
                ToolType = 0;
                this.toolImage.Visibility = System.Windows.Visibility.Collapsed;
                this.StoryboardTool.Stop();
                IsTooled = false;
                return true;
            }
            return false;
        }

        public void SetToolItem(int toolType)
        {
            ToolType = toolType;
            string uri = string.Empty;
            switch (toolType)
            {
                case 1:
                    uri = ToolSelector.bombImageUri;
                    break;
                case 2:
                    uri = ToolSelector.timeImageUri;
                    break;
                case 3:
                    uri = ToolSelector.colorImageUri;
                    break;
            }
            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            toolImage.Source = bitmap;
            this.toolImage.Visibility = System.Windows.Visibility.Visible;
            this.StoryboardTool.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardTool.Begin();
            IsTooled = true;
            IsSence = true;
        }

        public bool ClearSence()
        {
            if (IsSence)
            {
                this.path1.Visibility = System.Windows.Visibility.Collapsed;
                this.StoryboardToolSence.Stop();
                IsSence = false;
                return true;
            }
            return false;
        }

        public void SetSence()
        {
            this.path1.Visibility = System.Windows.Visibility.Visible;
            this.StoryboardToolSence.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardToolSence.Begin();
            IsSence = true;
        }
    }
}
